#! /bin/sh

# Write profile to DSP DAC and play a test tone

OUT=/dev/tty1

case "$1" in
    start)
	echo "Programming power controller"  > $OUT
	pyupdi -c /dev/ttyAMA0 -d mega808 -f /opt/hifiberry/contrib/firmware.hex >> $OUT
	if [ $? != 0 ]; then
	   echo "Failed to program board"
	fi
        echo "Writing configuration to controller" > $OUT
        modprobe i2c-dev
        sleep 3
        # Write basic configuration
        i2cset -y -f 1 0x77 1 0
	i2cset -y -f 1 0x77 2 0
	i2cset -y -f 1 0x77 3 0x7f
	i2cset -y -f 1 0x77 4 0
	i2cset -y -f 1 0x77 0x0a 6
	i2cset -y -f 1 0x77 0xfb 1
	sleep 2
	i2cset -y -f 1 0xff 3 0 
	i2cget -y -f 1 0x77 0xfb
	i2cset -y -f 1 0x77 0xfc 1
        T=`i2cget -y -f 1 0x77 0x0a`
        if [ "$T" != "0x06" ]; then
          echo ERROR, expect 6, but got $T > $OUT
          exit 1
        fi
        echo "OK, LED should now light green" > $OUT
	;;
    stop)
	;;
    restart)
	$0 stop
	$0 start
	;;
    *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
	;;
esac

